package com.qifenqian.turbot.module.presenter

import android.content.Context
import com.qifenqian.turbot.base.BasePresenter
import com.qifenqian.turbot.module.contract.DishContract
import com.qifenqian.turbot.module.entity.request.CategoryRequest
import com.qifenqian.turbot.module.entity.request.DishAddEditRequest
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.entity.response.dish.CategoryAndDishEntity
import com.qifenqian.turbot.module.entity.response.dish.CategoryEntity
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.qifenqian.turbot.module.model.DishModel
import com.qifenqian.turbot.net.ModelCallback

/**
 * @author H
 * @createTime 2018/12/27 14:25
 * @description
 * @see
 * @since
 */
class DishPresenter(context: Context, view: DishContract.View) : BasePresenter<DishContract.View, DishModel>(context, view), DishContract.Presenter {

    override fun uploadFile(filePath: String) {
        mModel.uploadFile(filePath, object : ModelCallback<UploadEntity> {
            override fun onSuccess(msg: String?, t: UploadEntity?) {
                mView?.uploadFileResult(true, msg = msg,uploadEntity = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.uploadFileResult(false, msg = msg)
            }
        })
    }

    override fun editDish(request: DishAddEditRequest) {
        mModel.editDish(request, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.editDishResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.editDishResult(false, code, msg)
            }
        })
    }

    override fun deleteDish(dishId: Int) {
        mModel.deleteDish(dishId, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.deleteDishResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.deleteDishResult(false, code, msg)
            }
        })
    }

    override fun saveCategory(request: CategoryRequest) {
        mModel.saveCategory(request, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.saveCategoryResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.saveCategoryResult(false, code, msg)
            }
        })
    }

    override fun deleteCategory(dishTypeId: Int, position: Int) {
        mModel.deleteCategory(dishTypeId, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.deleteCategoryResult(true, msg = msg, position = position)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.deleteCategoryResult(false, code, msg)
            }
        })
    }

    override fun editCategory(categoryName: String, dishTypeId: Int, position: Int) {
        mModel.editCategory(categoryName, dishTypeId, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.editCategoryResult(true, msg = msg, position = position, newCategory = categoryName)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.editCategoryResult(false, code, msg)
            }
        })
    }

    override fun addDish(request: DishAddEditRequest) {
        mModel.addDish(request, object : ModelCallback<DishEntity> {
            override fun onSuccess(msg: String?, t: DishEntity?) {
                mView?.addDishResult(true, msg = msg, dishEntity = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.addDishResult(false, code, msg)
            }
        })
    }

    override fun addCategory(dishTypeName: String) {
        mModel.addCategory(dishTypeName, object : ModelCallback<CategoryEntity> {
            override fun onSuccess(msg: String?, t: CategoryEntity?) {
                t!!.dishTypeName = dishTypeName
                mView?.addCategoryResult(true, msg = msg, categoryEntity = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.addCategoryResult(false, code, msg)
            }

        })
    }

    override fun getDishAndCategoryList() {
        mModel.getDishAndCategoryList(object : ModelCallback<Any> {
            override fun onSuccess(msg: String?, t: Any?) {
                val categoryAndDishEntity = t as CategoryAndDishEntity
                mView?.getDishAndCategoryListResult(true, msg = msg, categoryEntities = categoryAndDishEntity.categoryEntities, dishEntities = categoryAndDishEntity.dishEntities)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.getDishAndCategoryListResult(false, code, msg)
            }
        })

    }

    //    override fun getAllDishList() {
//        mModel.getAllDishList(object : ModelCallback<List<DishEntity>> {
//            override fun onSuccess(msg: String?, t: List<DishEntity>?) {
//                mView?.getAllDishListResult(true, dishEntities = t)
//            }
//
//            override fun onFailure(code: Int, msg: String?) {
//                mView?.getAllDishListResult(false, code, msg)
//            }
//        })
//    }
//
    override fun getCategoryList() {
        mModel.getCategoryList(object : ModelCallback<List<CategoryEntity>> {
            override fun onSuccess(msg: String?, t: List<CategoryEntity>?) {
                mView?.getCategoryListResult(true, msg = msg, categoryEntities = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.getCategoryListResult(false, code, msg)
            }
        })
    }

    override fun createModel(): DishModel {
        return DishModel(mContext)
    }
}
